<?php

namespace App\Validators\Buz;

use App\Models\PlatUser;
use App\Models\PlatUserApp;
use Illuminate\Contracts\Validation\Factory;
use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\Rule;
use \Prettus\Validator\Contracts\ValidatorInterface;
use \Prettus\Validator\LaravelValidator;

class AppValidator extends LaravelValidator
{


    protected $attributes = [
        'name' => '应用名称',
        'classify' => '应用类型',
        'domain' => '绑定域名',
        'icp' => 'icp备案号',
        'notify_url' => '异步通知地址',
        'app_signature' => '客户端签名',
        'app_package' => '客户端包名'
    ];

    public function __construct(\Illuminate\Contracts\Validation\Factory $validator)
    {
        parent::__construct($validator);
//        $app = PlatUserApp::where('app_id', request()->route('app'))->first(['id']);
//        $id = $app ? $app->id : 0;
        $this->rules = [
            ValidatorInterface::RULE_CREATE => [
                'name' => [
                    'required',
                    'max:150',
                    Rule::unique('plat_user_apps')->where(function ($query) {
                        return $query->where('deleted_at', null);
                    })
                ],
                'classify' => 'required|in:0,1,2',
//                'domain' => 'required|max:50|unique:plat_user_apps,domain|domain',
                'domain' => 'required|max:50|domain',
//                'icp' => 'required_if:classify,0,1|nullable|max:50|unique:plat_user_apps,icp',
                'icp' => 'required|nullable|max:50',
                'app_signature' => 'required_if:classify,1,2|nullable|max:50|unique:plat_user_apps,app_signature',
                'app_package' => 'required_if:classify,1|nullable|max:50|unique:plat_user_apps,app_package',
//                'notify_url' => 'required|max:200|active_url'
            ],
            ValidatorInterface::RULE_UPDATE => [
                'name' => [
                    'required',
                    'max:150',
                    Rule::unique('plat_user_apps')->where(function ($query) {
                        return $query->where('deleted_at', null);
                    })->ignore(request()->route('app'), 'app_id')
                ],
//                'notify_url' => 'required|max:200|active_url'
            ],
        ];
    }
}
